package org.yxj.requestlibaray.i;

import com.google.gson.Gson;
import org.yxj.requestlib.i.Callback;
import org.yxj.requestlib.manager.api.RequestManager;
import org.yxj.requestlibaray.bean.TokenResponse;
import java.util.Map;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class Apis implements ApisMethod{

    private String authorization = "";
    private String baseUrl = "";
    private static ApisMethod apiMethod ;

    private  Apis(){}

    public static ApisMethod newInstance() {
        if (null==apiMethod){
            synchronized (Apis.class){
                if (null==apiMethod){
                    apiMethod = new Apis();
                }
            }
        }
        return apiMethod ;
    }


    @Override
    public Apis baseUrl(String baseUrl) {
        this.baseUrl=baseUrl;
        return this;
    }

    @Override
    public Apis authorization(String authorization) {
        this.authorization = authorization;
        return this;
    }


    @Override
    public void TokenTask(Map<String, Object> map, final Callback<TokenResponse> callback) {
        RequestManager.getInstance()
                .init(baseUrl)
                .setAuthorization(authorization)
                .req(MeServer.class)
                .token(map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<TokenResponse>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        if (callback!=null){
                            callback.onFail(-1000,e.getMessage());
                        }
                    }

                    @Override
                    public void onNext(TokenResponse tokenResponse) {
                        if (callback!=null){
                            callback.onSuccess(tokenResponse);
                        }
                    }
                });
    }


    @Override
    public  void HomeGoodsListTask(Map<String, Object> map, final Callback<Object> callback) {
        RequestManager.getInstance()
                .init(baseUrl)
                .setAuthorization(authorization)
                .req(MeServer.class)
                .HomeGoodsListTask(map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        if (callback!=null){
                            callback.onFail(-1000,e.getMessage());
                        }
                    }

                    @Override
                    public void onNext(Object o) {
                        String result = new Gson().toJson(o) ;
                        if (callback!=null){
                            callback.onSuccess(o);
                        }
                    }
                });
    }

    @Override
    public void LoginByPasswordTask(Map<String, Object> map, final Callback<Object> callback) {
        RequestManager.getInstance()
                .init(baseUrl)
                .setAuthorization(authorization)
                .req(MeServer.class)
                .loginByPasswordTask(map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        if (callback!=null){
                            callback.onFail(-1000,e.getMessage());
                        }
                    }

                    @Override
                    public void onNext(Object o) {
                        String result = new Gson().toJson(o) ;
                        if (callback!=null){
                            callback.onSuccess(o);
                        }
                    }
                });
    }
}
